// by Xeno
#define THIS_FILE "fn_calldrop.sqf"
#include "x_setup.sqf"
if (!X_Client) exitWith {
	GVAR(commandingMenuIniting) = false;
};
private ["_array","_control","_display","_oldpos","_exitIt"];

if (!alive player) exitWith {
	GVAR(commandingMenuIniting) = false;
};
if (vehicle player == player && {position player select 2 > 10}) exitWith {
	GVAR(commandingMenuIniting) = false;
};

if (!__XJIPGetVar(para_available)) exitWith {
	(localize "STR_DOM_MISSIONSTRING_163") call FUNC(HQChat);
	GVAR(commandingMenuIniting) = false;
};

if (GVAR(with_ranked) && {score player < (GVAR(ranked_a) select 16)}) exitWith {
	(format [(localize "STR_DOM_MISSIONSTRING_164"), score player, GVAR(ranked_a) select 16]) call FUNC(HQChat);
	GVAR(commandingMenuIniting) = false;
};

if ((GVAR(with_ai) || {GVAR(with_ai_features) == 0}) && {__XJIPGetVar(GVAR(drop_blocked))}) exitWith {
	(localize "STR_DOM_MISSIONSTRING_165") call FUNC(HQChat);
	GVAR(commandingMenuIniting) = false;
};

// TYRGHEN
if (time < (__XJIPGetVar(para_next_time))) exitWith {
	(format[localize "STR_DOM_MISSIONSTRING_2025",ceil((__XJIPGetVar(para_next_time)) - time)]) call FUNC(HQChat);
	GVAR(commandingMenuIniting) = false;
};

if (GVAR(with_ai)|| {GVAR(with_ai_features) == 0}) then {
	[QGVAR(drop_blocked),true] call FUNC(NetSetJIP);
};

["arti1_marker_1",getPosASL player,"ELLIPSE","ColorYellow",[GVAR(drop_max_dist), GVAR(drop_max_dist)],"",0,"","FDiagonal"] call FUNC(CreateMarkerLocal);

GVAR(x_drop_type) = "";
_oldpos = getPosASL GVAR(X_DropZone);
disableSerialization;
createDialog "XD_AirDropDialog";
GVAR(commandingMenuIniting) = false;
#define CTRL(A) (_display displayCtrl A)
_display = __uiGetVar(D_DROP_DIALOG);
_array = GVAR(x_drop_array) select 0;
CTRL(11002) ctrlSetText (_array select 0);

_array = GVAR(x_drop_array) select 0;
if (count _array == 0) then {
	ctrlShow [11002, false];
} else {
	CTRL(11002) ctrlSetText (_array select 0);
};
_array = GVAR(x_drop_array) select 1;
if (count _array == 0) then {
	ctrlShow [11003, false];
} else {
	CTRL(11003) ctrlSetText (_array select 0);
};
_array = GVAR(x_drop_array) select 2;
if (count _array == 0) then {
	ctrlShow [11004, false];
} else {
	CTRL(11004) ctrlSetText (_array select 0);
};

/*if (count GVAR(x_drop_array) > 1) then {
	_array = GVAR(x_drop_array) select 1;
	CTRL(11003) ctrlSetText (_array select 0);
} else {
	ctrlShow [11003, false];
	ctrlShow [11004, false];
};
if (count GVAR(x_drop_array) > 2) then {
	_array = GVAR(x_drop_array) select 2;
	CTRL(11004) ctrlSetText (_array select 0);
};
*/

waitUntil {GVAR(x_drop_type) != "" || {!GVAR(airdrop_dialog_open)} || {!alive player}};
deleteMarkerLocal "arti1_marker_1";
if (!alive player) exitWith {if (GVAR(airdrop_dialog_open)) then {closeDialog 0}};
sleep 0.5;
if (GVAR(x_drop_type) != "") then {
	_ppl = getPosASL player;
	_ppl set [2,0];
	// TYRGHEN - Added check for drops away from active side mission and active town
	_is_valid_pos = true;
	if (_is_valid_pos && !isnil QGVAR(x_sm_pos)) then {
		if (count GVAR(x_sm_pos) > 0) then {
			_is_valid_pos = (((GVAR(x_sm_pos) select 0) distance _ppl) >= GVAR(drop_min_dist));
		};
	};
	_target_index = __XJIPGetVar(GVAR(current_target_index));
	if (isnil "_target_index") then { _target_index = -1 };
	if (_is_valid_pos && _target_index != -1) then {
		_target_array = GVAR(target_names) select (X_JIPH getVariable QGVAR(current_target_index));
		_is_valid_pos = (((_target_array select 0) distance _ppl) >= GVAR(drop_min_dist));
	};
	if (!_is_valid_pos) exitWith {
		(format [(localize "STR_DOM_MISSIONSTRING_2023"), GVAR(drop_min_dist)]) call FUNC(HQChat);
		GVAR(x_dropzone) setPosASL _oldpos;
		QGVAR(drop_zone) setMarkerPos _oldpos;
	};

	if (_ppl distance [getPosASL GVAR(X_DropZone) select 0, getPosASL GVAR(X_DropZone) select 1, 0] > GVAR(drop_max_dist)) exitWith {
		(format [(localize "STR_DOM_MISSIONSTRING_166"), GVAR(drop_max_dist)]) call FUNC(HQChat);
		GVAR(x_dropzone) setPosASL _oldpos;
		QGVAR(drop_zone) setMarkerPos _oldpos;
	};
	[player, format [(localize "STR_DOM_MISSIONSTRING_167"), [GVAR(x_drop_type), 0] call FUNC(GetDisplayName)]] call FUNC(SideChat);
	if (GVAR(with_ranked)) then {[QGVAR(pas), [player, (GVAR(ranked_a) select 16) * -1]] call FUNC(NetCallEventCTS)};
	[QGVAR(x_dr_t), [GVAR(x_drop_type), markerPos QGVAR(drop_zone), player]] call FUNC(NetCallEventCTS);
	// TYRGHEN
	[QUOTE(para_next_time),(time + GVAR(drop_min_interval))] call FUNC(NetSetJIP);
} else {
	(localize "STR_DOM_MISSIONSTRING_168") call FUNC(HQChat);
	GVAR(x_dropzone) setPosASL _oldpos;
	QGVAR(drop_zone) setMarkerPos _oldpos;
};

if (GVAR(with_ai)|| {GVAR(with_ai_features) == 0}) then {
	[QGVAR(drop_blocked),false] call FUNC(NetSetJIP);
};